package com.prince.web.service.impl;

import com.alibaba.fastjson.JSON;
import com.prince.bean.CommonWebResponse;
import com.prince.bean.ResultParams;
import com.prince.bean.WebCode;
import com.prince.mapper.CodeMapper;
import com.prince.mapper.CodeSortMapper;
import com.prince.mapper.x.CodeSortMapperX;
import com.prince.model.CodeExample;
import com.prince.model.CodeSort;
import com.prince.model.CodeSortExample;
import com.prince.page.Page;
import com.prince.util.JsonUtil;
import com.prince.util.StringUtil;
import com.prince.web.base.BaseService;
import com.prince.web.service.CodeSortService;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.Date;
import java.util.List;
import java.util.Map;

@Service
public class CodeSortServiceImpl extends BaseService implements CodeSortService {

	@Resource
	private CodeSortMapperX codeSortMapperX;
	
	@Resource
	private CodeSortMapper codeSortMapper;
	
	@Resource
	private CodeMapper codeMapper;

//	@Resource
//	private CodeCache codeCache;

	@Override
	public Map<String, Object> findCodeSortByPage(Map<String, Object> paramMap, Page page) {
		ResultParams response = CommonWebResponse.getSuccessResponseCodeMsgParams();
          //判断参数是否为空
		if (null == paramMap || paramMap.size() == 0) {
			logger.debug("[CodeSortServiceImpl] findCodeSortByPage param beanJson is null");
			return CommonWebResponse.getCommonRespCodeMsgParams(WebCode.systemError).toHashMap();
		}

		if (null == page) {
			logger.debug("[CodeSortServiceImpl] findCodeSortByPage page is null");
			return CommonWebResponse.getCommonRespCodeMsgParams(WebCode.systemError).toHashMap();
		}
        logger.debug("[CodeSortServiceImpl] findCodeSortByPage 参数为"+paramMap);
        //创建criteria
		CodeSortExample codeSortExample = new CodeSortExample();
		CodeSortExample.Criteria criteria = codeSortExample.createCriteria();
		// if(MapUtil.isNotEmpty(paramMap, "code")){
		// criteria.andCodeEqualTo(paramMap.get("code").toString());
		// }
		paramMap.remove("startTime");
		paramMap.remove("page");
		paramMap.remove("endTime");
		paramMap.remove("rows");
        //反射封装参数
		StringUtil.reflectMap(criteria, paramMap);
        //查总记录数
		int total = codeSortMapperX.countByExample(codeSortExample);
        logger.debug("[CodeSortServiceImpl] findCodeSortByPage 总记录数"+total);
		codeSortExample.setPage(page);
		codeSortExample.setOrderByClause("cteated desc");
		List<CodeSort> list = codeSortMapperX.selectByExample(codeSortExample);
		 logger.debug("[CodeSortServiceImpl] findCodeSortByPage 返回结果"+JsonUtil.toJSONString(list));
		return response.toHashMapByPage(total, list);
	}

	@Override
	public Map<String, Object> addCodeSort(CodeSort codeSort) {
		CodeSortExample codeSortExample = new CodeSortExample();
		CodeSortExample.Criteria criteria = codeSortExample.createCriteria();
		//添加时，查询是否重名
		if (StringUtil.isNotEmpty(codeSort.getName())) {
			criteria.andNameEqualTo(codeSort.getName());
		}

		List<CodeSort> codeSortList = codeSortMapperX.selectByExample(codeSortExample);
		if (!codeSortList.isEmpty()) {
			logger.debug("[addcodeSort] insert codeSort duplicate name. codeSort={}", JSON.toJSONString(codeSort));
			return CommonWebResponse.getCommonRespCodeMsgParams(WebCode.duplicateName).toHashMap();
		}
		ResultParams response = CommonWebResponse.getSuccessResponseCodeMsgParams();
		codeSort.setCteated(new Date());
		logger.debug("[CodeSortServiceImpl] addCodeSort 参数是"+JsonUtil.toJSONString(codeSort));
		int i = codeSortMapperX.insertSelective(codeSort);
		//清空缓存
//		codeCache.del();
		if (!(i > 0)) {
			logger.debug("[addCodeSort] insert CodeSort is fail. notice={}", JSON.toJSONString(codeSort));
			return CommonWebResponse.getCommonRespCodeMsgParams(WebCode.systemError).toHashMap();
		}
		return response.toHashMap();
	}

	@Override
	public Map<String, Object> delCodeSort(CodeSort codeSort) {
		ResultParams response = CommonWebResponse.getSuccessResponseCodeMsgParams();
        //根据id删除字典分类
		codeSort = codeSortMapper.selectByPrimaryKey(codeSort.getId());
		logger.debug("[CodeSortServiceImpl] delCodeSort id={}"+codeSort.getId());
		int i = codeSortMapperX.deleteByPrimaryKey(codeSort.getId());
		//删除code表
		CodeExample codeExample = new CodeExample();
		codeExample.createCriteria().andSortCodeEqualTo(codeSort.getCode());
		int j = codeMapper.deleteByExample(codeExample);
		if (!(j > 0)) {
			logger.debug("[delCodeSort] 删除code 表结果：" + j);
		}
		//清空缓存
//		codeCache.del();
		if (!(i > 0)) {
			logger.debug("[delCodeSort] del CodeSort is fail id={}", codeSort.getId());
			return CommonWebResponse.getCommonRespCodeMsgParams(WebCode.systemError).toHashMap();
		}
		return response.toHashMap();
	}

	@Override
	public Map<String, Object> updateCodeSort(CodeSort codeSort) {
		ResultParams response = CommonWebResponse.getSuccessResponseCodeMsgParams();
		//更新时，判断名称是否被修改，如果修改了，查询是否重名
		if (StringUtil.isNotEmpty(codeSort.getName()) && StringUtil.isNotEmpty(codeSort.getRemark())
				&& !codeSort.getName().equals(codeSort.getRemark())) {
			CodeSortExample codeSortExample = new CodeSortExample();
			codeSortExample.createCriteria().andNameEqualTo(codeSort.getName());
			List<CodeSort> list = codeSortMapperX.selectByExample(codeSortExample);
			if (!list.isEmpty()) {
				codeSort.setRemark(null);
				return CommonWebResponse.getCommonRespCodeMsgParams(WebCode.duplicateName).toHashMap();
			}
		}
		logger.debug("[CodeSortServiceImpl] updateCodeSort 参数为"+JsonUtil.toJSONString(codeSort));
		int i = codeSortMapperX.updateByPrimaryKeySelective(codeSort);
		//清空缓存
//		codeCache.del();
		if (!(i > 0)) {
			logger.debug("[updateCodeSort] update CodeSort is fail id={}", codeSort.getId());
			return CommonWebResponse.getCommonRespCodeMsgParams(WebCode.systemError).toHashMap();
		}

		return response.toHashMap();
	}

	@Override
	public Map<String, Object> findCodeSortByCondition(CodeSort codeSort) {
		ResultParams response = CommonWebResponse.getSuccessResponseCodeMsgParams();
         //添加条件查询
		CodeSortExample codeSortExample = new CodeSortExample();
		CodeSortExample.Criteria criteria = codeSortExample.createCriteria();

		if (null != codeSort.getId()) {
			criteria.andIdEqualTo(codeSort.getId());
		}
		if (null != codeSort.getCode() && !codeSort.getCode().equals("")) {
			criteria.andCodeEqualTo(codeSort.getCode());
		}
        logger.debug("[CodeSortServiceImpl] findCodeSortByCondition 参数为"+JsonUtil.toJSONString(codeSort));
		List<CodeSort> list = codeSortMapperX.selectByExample(codeSortExample);
        //判断是否为空
		if (null == list || list.size() == 0) {
			logger.debug("[findCodeSortByCondition] find codeSortList is null");
			return CommonWebResponse.getCommonRespCodeMsgParams(WebCode.systemError).toHashMap();
		}
		logger.debug("[CodeSortServiceImpl] findCodeSortByConditio 返回结果"+JsonUtil.toJSONString(list));
		response.put("list", list);
		return response.toHashMap();
	}

	@Override
	public Map<String, Object> dqueryCodeSort(CodeSort codeSort) {
		ResultParams response = CommonWebResponse.getSuccessResponseCodeMsgParams();
        //创建查询criteria
		CodeSortExample codeSortExample = new CodeSortExample();
		CodeSortExample.Criteria criteria = codeSortExample.createCriteria();

		// 模糊查询条条件 number,name
		if (StringUtil.isNotEmpty(codeSort.getCode())) {

			criteria.andCodeLike("%" + codeSort.getCode() + "%");

		}
		if (StringUtil.isNotEmpty(codeSort.getName())) {

			criteria.andNameLike("%" + codeSort.getName() + "%");

		}
        //分页参数
		Page page = new Page();
		page.setBegin(0);
		page.setLength(10);
		codeSortExample.setPage(page);
         logger.debug("[CodeSortServiceImpl] dqueryCodeSort 参数"+JsonUtil.toJSONString(codeSort));
		List<CodeSort> list = codeSortMapperX.selectByExample(codeSortExample);
       //判断是否为空
		if (null == list || list.size() == 0) {
			logger.debug("[dqueryCodeSort] find codeSortList is null");
			return CommonWebResponse.getCommonRespCodeMsgParams(WebCode.systemError).toHashMap();
		}
		logger.debug("[CodeSortServiceImpl] dqueryCodeSort 返回结果"+JsonUtil.toJSONString(list));
		response.put("list", list);
		return response.toHashMap();
	}

}
